import 'dart:io';

import 'package:flutter/material.dart';

/// 扩展放这里
extension BuildContextExt on BuildContext {
  /// 获取当前组件的 RenderBox
  RenderBox? renderBox() {
    return findRenderObject() is RenderBox
        ? (findRenderObject() as RenderBox)
        : null;
  }

  /// 当前组件相对位置在整个屏幕的位置
  Offset? position({Offset offset = Offset.zero}) {
    try {
      return renderBox()?.localToGlobal(offset);
    } catch (e) {
      return null;
    }
  }

  /// 键盘是否可见
  bool get keyboardVisible => MediaQuery.of(this).viewInsets.bottom > 200;
}

extension GlobalKeyExt on GlobalKey {
  /// 获取当前组件的 RenderBox
  RenderBox? renderBox() => currentContext?.renderBox();

  /// 获取当前组件的 Position
  Offset? position({Offset offset = Offset.zero}) =>
      currentContext?.position(offset: offset);

  /// 获取当前组件的 Size
  Size? get size => currentContext?.size;
}

extension FileSystemEntityExt on FileSystemEntity {
  String get fileName =>
      path.substring(path.lastIndexOf(Platform.isWindows ? "\\" : "/") + 1);

  String get parentPath =>
      path.substring(0, path.lastIndexOf(Platform.isWindows ? "\\" : "/") + 1);
}
